package com.gismore.demo.web;

import com.gismore.demo.common.RET;
import com.gismore.demo.common.constants.FConst;
import com.gismore.demo.db.domain.SecUser;
import com.gismore.demo.db.service.ISecUserService;
import com.google.common.base.Preconditions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * Created by lei on 2017/5/26.
 */
@RestController
public class LoginController {

	@Autowired
	private ISecUserService secUserService;

	@RequestMapping(value = "/login",method = RequestMethod.POST)
	public RET login(@RequestParam(FConst.PARAMS_USERNAME)String username,@RequestParam(FConst.PARAMS_PASSWORD)String password){
		SecUser secUser = null;
		try {
			secUser = secUserService.findByUser(username, password);
			Preconditions.checkNotNull(secUser,"用户名或者密码无效！");
		} catch (Exception e) {
			e.printStackTrace();
			return new RET(e);
		}
		return new RET(secUserService.generateToken(secUser));
	}
}
